package com.example.demo.controller;

import com.example.demo.utils.User;
import com.example.demo.utils.LoginUser;
import com.example.demo.service.UserService;
import com.example.demo.utils.ApiResponse;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
public class UserController {

    @Autowired
    private UserService userService;

    @PostMapping("/login")
    public ApiResponse userLogin(@RequestBody LoginUser user) {
        return userService.userLogin(user);
    }


    @PostMapping("/register")
    public synchronized ApiResponse userRegister(@RequestBody User user) {
        return userService.userRegister(user);
    }

    @ApiImplicitParams({
            @ApiImplicitParam(name = "token", value = "Token for authentication", required = true, dataType = "string", paramType = "header")
    })
    @GetMapping("/api/user/info")
    public ApiResponse getUserInfo(@RequestHeader("token") String token) {
        return userService.getUserInfo(token);
    }

    @ApiImplicitParams({
            @ApiImplicitParam(name = "token", value = "Token for authentication", required = true, dataType = "string", paramType = "header")
    })
    @PutMapping("/api/user/update")
    public ApiResponse updateUserInfo(@RequestHeader("token") String token, @RequestBody User updatedUser) {
        return userService.updateUserInfo(token, updatedUser);
    }

    @ApiImplicitParams({
            @ApiImplicitParam(name = "token", value = "Token for authentication", required = true, dataType = "string", paramType = "header")
    })
    @GetMapping("/api/user/orders")
    public ApiResponse getUserOrders(@RequestHeader("token") String token) {
        return userService.getUserOrders(token);
    }


}
